//
//  CLBehindeView.m
//  chanlin
//
//  Created by Ryan on 15-1-28.
//  Copyright (c) 2015年 chanlin. All rights reserved.
//

#import "CLBehindeView.h"
#import "UIImageView+WebCache.h"
#import "ClEntityHead.h"
#import "CLSlidersViewController.h"
#import "CLSystemPushAnimated.h"
#import "UIView+Utils.h"

@interface CLBehindeView ()
@property (nonatomic, strong) UIButton *backButton;
@property (nonatomic, strong) UILabel *moreLabel;
@property (nonatomic, strong) UIButton *nextSliderBtn;

@end
@implementation CLBehindeView

- (instancetype)init{
    self=[super init];
    if(self){
        self.relationView=[[UIView alloc] init];
     
        self.relationView.frame=CGRectMake(0, 130, GlobleWidth, GlobleHeight);
        [self addSubview:self.relationView];
        
        self.backButton=[UIButton buttonWithType:UIButtonTypeCustom];
        self.backButton.frame=CGRectMake(5.f, 26.f, 30.f, 30.f);
        self.backButton.contentEdgeInsets=UIEdgeInsetsMake(3.f, 3.f, 3.f, 3.f);
        [self.backButton setImage:[UIImage imageNamed:@"slider_back.png"]
                         forState:UIControlStateNormal];
        [self addSubview:self.backButton];
        [self.backButton addTarget:self action:@selector(backList) forControlEvents:UIControlEventTouchUpInside];

        
        _moreLabel = [[UILabel alloc] initWithFrame:CGRectMake((GlobleWidth-80)/2, 32, 80, 18.f)];
        _moreLabel.backgroundColor = [UIColor clearColor];
        [_moreLabel setText:@"推荐图集"];
        [_moreLabel setTextAlignment:NSTextAlignmentCenter];
        [_moreLabel setTextColor:[UIColor whiteColor]];
        [_moreLabel setFont:[UIFont systemFontOfSize:17.f]];
        [self addSubview:_moreLabel];
        
        NSMutableArray *array=[NSMutableArray array];
        for(int i=0;i<4;i++){
            UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
            button.tag=i;
            button.frame=CGRectMake(11+149*(i%2), (i/2)*145, 143.0, 158.0);
            
            button.backgroundColor = [UIColor clearColor];
            
            UIImageView *img=[[UIImageView alloc] init];
            img.frame=CGRectMake(0, 0, 143.0, 107.0);
            img.tag=20;
            [button addSubview:img];
            UILabel *title=[[UILabel alloc] init];
            title.font=[UIFont systemFontOfSize:12.f];
            title.textColor=RGB(0xb5, 0xb5, 0xb5);
            title.numberOfLines=2;
            title.frame=CGRectMake(0, img.bottom+8, 143.f, title.font.lineHeight*2+5);
            title.tag=21;
            [button addSubview:title];
            [self.relationView addSubview:button];
            
            [array addObject:button];
            
            [button addTarget:self action:@selector(tap:) forControlEvents:UIControlEventTouchUpInside];
        }
        self.buttons=array;
        
        //点击进入下一图集
        _nextSliderBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _nextSliderBtn.frame = CGRectMake(0, 366, GlobleWidth, 35);
        [_nextSliderBtn addTarget:self action:@selector(goToNextSlider:) forControlEvents:UIControlEventTouchUpInside];
        [self.relationView addSubview:_nextSliderBtn];
        
        UILabel *nextLab = [[UILabel alloc]init];
        nextLab.tag = 30;
        nextLab.backgroundColor = [UIColor clearColor];
        nextLab.frame = CGRectMake((GlobleWidth-115)/2-5, 0, 115, 14);
        nextLab.text = @"点击进入下一图集";
        nextLab.textColor = [UIColor whiteColor];
        nextLab.font = [UIFont systemFontOfSize:14.f];
        nextLab.textAlignment = NSTextAlignmentLeft;
        [_nextSliderBtn addSubview:nextLab];
        
        UIImageView *arrowImg = [[UIImageView alloc]init];
        arrowImg.tag = 31;
        arrowImg.frame = CGRectMake(nextLab.right+2, 1, 12, 12);
        [arrowImg setImage:[UIImage imageNamed:@"slider_relation_next_slider.png"]];
        [_nextSliderBtn addSubview:arrowImg];
        
        UILabel *nextSliderTitle = [[UILabel alloc]init];
        nextSliderTitle.tag = 32;
        nextSliderTitle.frame = CGRectMake(0, 22, GlobleWidth, 11);
        nextSliderTitle.textColor = [UIColor whiteColor];
        nextSliderTitle.font = [UIFont systemFontOfSize:11.f];
        nextSliderTitle.textAlignment = NSTextAlignmentCenter;
        [_nextSliderBtn addSubview:nextSliderTitle];
        
       
   
    }
    return self;
}

- (void)goToNextSlider:(UIButton *)button {
    NSInteger type=[[self.next sgrGetStringForKey:@"type"] integerValue];
    if(type==CLList_sliders){
        CLSlidersViewController *slider=[CLSlidersViewController new];
        slider.ref=@"behind";
        slider.itemId=[self.next sgrGetStringForKey:@"id"];
        [[CLSystemPushAnimated sharedInstance] pushController:slider];
        
    }
}

- (void) backList {
   [[self k_currentController].dispatchObj pop];
}

- (void)loadArray:(NSArray *)array{
    self.relations=array;
    NSInteger n=[array count]>4?4:array.count;
    for (int i=0; i<n; i++) {
        NSDictionary *dic=[array sgrGetDictionaryForIndex:i];
        UIButton *button=[self.buttons sgrGetType:[UIButton class] forIndex:i];
        UIImageView *img=(UIImageView *)[button viewWithTag:20];
        if(_isStrNotNull([dic sgrGetStringForKey:@"titlepic"]))
        [img sd_setImageWithURL:[NSURL URLWithString:[dic sgrGetStringForKey:@"titlepic"]]];
        
        UILabel *label=(UILabel *)[button viewWithTag:21];
        label.text=[dic sgrGetStringForKey:@"title"];
    }
}

- (void)loadNextDic:(NSDictionary *)dic{
    self.next = dic;
    UILabel *label=(UILabel *)[_nextSliderBtn viewWithTag:32];
    label.text = [dic sgrGetStringForKey:@"title"];
    if(_isStrNULL([self.next sgrGetStringForKey:@"id"])){
        _nextSliderBtn.hidden=YES;
    }else{
        _nextSliderBtn.hidden=NO;
    }
}

- (void)layoutSubviews{
    if([UIDevice currentDevice].orientation==UIDeviceOrientationLandscapeLeft||
       [UIDevice currentDevice].orientation==UIDeviceOrientationLandscapeRight){
        self.frame=CGRectMake(0, 0, GlobleHeight, GlobleWidth);
        self.relationView.frame=CGRectMake(0, 85, GlobleHeight, GlobleWidth);
        
        _moreLabel.frame = CGRectMake((GlobleHeight-80)/2, 30, 80, 18.f);
        self.backButton.frame=CGRectMake(5.f, 24.f, 30.f, 30.f);
        for (int i=0; i<self.buttons.count; i++) {
            UIButton *button=[self.buttons sgrGetType:[UIButton class] forIndex:i];
            button.frame=CGRectMake((8+132)*i+8, 0, 132.0, 185.0);
            UIImageView *img=(UIImageView *)[button viewWithTag:20];
            img.frame=CGRectMake(0, 0, 132.0, 99.0);
            UILabel *label=(UILabel *)[button viewWithTag:21];
            label.frame=CGRectMake(0, img.bottom+8, 132.f, label.font.lineHeight*2+5);
            
            
           
        }
        _nextSliderBtn.frame = CGRectMake(0, 176, GlobleHeight, 35);
        UILabel *nextLabel=(UILabel *)[_nextSliderBtn viewWithTag:30];
        nextLabel.frame = CGRectMake((GlobleHeight-115)/2-5, 0, 115, 14);
        UIImageView *img=(UIImageView *)[_nextSliderBtn viewWithTag:31];
        img.frame=CGRectMake(nextLabel.right+2, 1, 12, 12);
        UILabel *TitleLabel=(UILabel *)[_nextSliderBtn viewWithTag:32];
        TitleLabel.frame=CGRectMake(0, 22, GlobleHeight, 11);
        
        
    }else{
        self.frame=CGRectMake(0, 0, GlobleWidth, GlobleHeight);
        self.relationView.frame=CGRectMake(0, 130, GlobleWidth, GlobleHeight);
        _moreLabel.frame = CGRectMake((GlobleWidth-80)/2, 32, 80, 18.f);
        self.backButton.frame=CGRectMake(5.f, 26.f, 30.f, 30.f);
        for (int i=0; i<self.buttons.count; i++) {
            UIButton *button=[self.buttons sgrGetType:[UIButton class] forIndex:i];
            button.frame=CGRectMake(11+149*(i%2), (i/2)*145, 143.0, 158.0);
            UIImageView *img=(UIImageView *)[button viewWithTag:20];
            img.frame=CGRectMake(0, 0, 143.0, 107.0);
            UILabel *label=(UILabel *)[button viewWithTag:21];
            label.frame=CGRectMake(0, img.bottom+8, 143.f, label.font.lineHeight*2+5);
        }
        _nextSliderBtn.frame = CGRectMake(0, 366, GlobleWidth, 35);
        UILabel *nextLabel=(UILabel *)[_nextSliderBtn viewWithTag:30];
        nextLabel.frame = CGRectMake((GlobleWidth-115)/2-5, 0, 115, 14);
        UIImageView *img=(UIImageView *)[_nextSliderBtn viewWithTag:31];
        img.frame=CGRectMake(nextLabel.right+2, 1, 12, 12);
        UILabel *TitleLabel=(UILabel *)[_nextSliderBtn viewWithTag:32];
        TitleLabel.frame=CGRectMake(0, 22, GlobleWidth, 11);
    }
}

- (void)tap:(UIButton *)button{
    NSDictionary *dic=[self.relations sgrGetDictionaryForIndex:button.tag];
    NSInteger type=[[dic sgrGetStringForKey:@"type"] integerValue];
    if(type==CLList_sliders){
        CLSlidersViewController *slider=[CLSlidersViewController new];
        slider.ref=@"behind";
        slider.itemId=[dic sgrGetStringForKey:@"id"];
        [[CLSystemPushAnimated sharedInstance] pushController:slider];
        
    }
}



@end
